package cn.hospital.controller;

import cn.hospital.entity.MeaningUser;
import cn.hospital.service.LoginRegisteredService;
import cn.hospital.service.MeaningUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/loginRegistered")
public class LoginRegisteredController {
    @Autowired
    private LoginRegisteredService loginRegisteredService;
    @Resource
    private MeaningUserService meaningUserService;

    /**
     * 查重，手机号重复
     * @param phone
     * @return
     */
    @ResponseBody
    @RequestMapping("/findByUserPhone")
    public Map<String, Object> findByUserPhone(String phone) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("count", loginRegisteredService.findByPhone(phone));
        return map;
    }

    /**
     * 查重，邮箱重复
     * @param email 邮箱
     * @return
     */
    @ResponseBody
    @RequestMapping("/findByUserEmail")
    public Map<String, Object> findByUserEmail(String email) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("count", loginRegisteredService.findByEmail(email));
        return map;
    }

    /**
     * 注册方法
     * @param meaningUser 实体类
     * @return
     */
    @ResponseBody
    @RequestMapping("/addMeaningUser")
    public Map<String, Object> addMeaningUser(MeaningUser meaningUser) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("count", loginRegisteredService.addByMeaningUser(meaningUser));
        return map;
    }

    /**
     * 登录方法
     * @param name     用户名，电话，邮箱
     * @param password 密码
     * @return
     */
    @ResponseBody
    @RequestMapping("/findByUsernamePassword")
    public Map<String, Object> findByUsernamePassword(String name, String password, HttpSession session) {
        return loginRegisteredService.findByMeaningUser(name, password, session);
    }

    /**
     * 点击退出按钮，销毁session
     * @param session
     * @return
     */
    @RequestMapping("/exit")
    @ResponseBody
    public Map<String, Object> exit(HttpSession session) {
        //销毁session
        session.invalidate();
        Map<String, Object> map = new HashMap<String, Object>();
        return map;
    }

    /**
     *旧密码验证
     * @param password
     * @param id
     * @return
     */
    @RequestMapping("/eq")
    @ResponseBody
    public int eq(String password,Integer id){
        int sjak=loginRegisteredService.eq(password,id);
        return sjak;
    }

    /**
     *修改登录人信息，查询登录人详细信息
     * @param session
     * @return
     */
    @RequestMapping("/findMeangingUser")
    public String findMeangingUser(HttpSession session){
        MeaningUser meaningUser = (MeaningUser) session.getAttribute("meaningUser");
        Integer userId = meaningUser.getUserId();
        session.setAttribute("meaningUser",meaningUserService.selectuserbyid(userId));
        return "page/system/updateMeangingUser";
    }

    /**
     *修改登录人信息
     * @param meaningUser
     * @return
     */
    @RequestMapping("/updateMeaningUser")
    public String updateMeaningUser(MeaningUser meaningUser){
        meaningUserService.upMeaningUser(meaningUser);
        return "redirect:/loginRegistered/findMeangingUser";
    }


}
